package com.aggregate.pay.saobei.support.client;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aggregate.pay.saobei.bean.QueryRefundData;
import com.aggregate.pay.saobei.bean.QueryRefundResult;
import com.aggregate.pay.saobei.support.AbstractClient;
import com.aggregate.pay.saobei.support.Factory;
import com.aggregate.pay.saobei.support.Merch;
import com.aggregate.pay.saobei.support.utils.MD5;

/**
 * 退款查询
 * @author Moyq5
 * @date 2021年9月16日
 */
public class QueryRefund extends AbstractClient<QueryRefundData, QueryRefundResult> {
	
	private static final Logger log = LoggerFactory.getLogger(QueryRefund.class);
	
	private Merch merch;
	
	public QueryRefund(Merch merch) {
		super(merch);
		this.merch = merch;
	}

	@Override
	protected Class<QueryRefundResult> getResultClass() {
		return QueryRefundResult.class;
	}

	@Override
	protected String getApiPath() {
		return Factory.getConfig().serverPath() + "/pay/100/queryrefund";
	}

	@Override
	protected String sign(QueryRefundData data) {
		// 按固定字段固定顺序生成签名
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("pay_ver", data.getPayVer());
		map.put("pay_type", data.getPayType());
		map.put("service_id", data.getServiceId());
		map.put("merchant_no", data.getMerchantNo());
		map.put("terminal_id", data.getTerminalId());
		map.put("terminal_trace", data.getTerminalTrace());
		map.put("terminal_time", data.getTerminalTime());
		if (isEmpty(data.getOutRefundNo())) {
			data.setOutRefundNo("");
		}
		map.put("out_refund_no", data.getOutRefundNo());
		
		StringBuffer sb = new StringBuffer();
		for (Entry<String, Object> entry: map.entrySet()) {
			sb.append(entry.getKey() + "=" + (null == entry.getValue() ? "": String.valueOf(entry.getValue())) + "&");
		}
		sb.append("access_token=" + merch.accessToken());

		log.debug("签名内容：{}", sb.toString());
		String sign = MD5.MD5Encode(sb.toString()).toUpperCase();
		log.debug("签名值：{}", sign);
		data.setKeySign(sign);
		
		return sign;
	}

	@Override
	protected String sign(QueryRefundResult result) {
		if (null == result.getReturnCode() || !result.getReturnCode().equals("01")) {
			return result.getKeySign();
		}
		// 按固定字段固定顺序生成签名
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("return_code", result.getReturnCode());
		map.put("return_msg", result.getReturnMsg());
		map.put("result_code", result.getResultCode());
		map.put("pay_type", result.getPayType());
		map.put("merchant_name", result.getMerchantName());
		map.put("merchant_no", result.getMerchantNo());
		map.put("terminal_id", result.getTerminalId());
		map.put("terminal_trace", result.getTerminalTrace());
		map.put("terminal_time", result.getTerminalTime());
		map.put("refund_fee", result.getRefundFee());
		map.put("end_time", result.getEndTime());
		map.put("out_refund_no", result.getOutRefundNo());
		map.put("out_trade_no", result.getOutTradeNo());
		StringBuffer sb = new StringBuffer();
		map.forEach((key, value) -> {
			sb.append(key + "=" + (null == value ? "": String.valueOf(value)) + "&");
		});
		sb.deleteCharAt(sb.length() - 1);
		//sb.append("access_token=" + merch.accessToken());

		log.debug("签名内容：{}", sb.toString());
		String sign = MD5.MD5Encode(sb.toString()).toUpperCase();
		log.debug("签名值：{}", sign);
		
		return sign;
	}
	

	private boolean isEmpty(Object obj) {
		return null == obj || String.valueOf(obj).trim().isEmpty();
	}

}
